package com.dyz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyz.domain.SysRole;
import com.dyz.model.Result;
import com.dyz.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/roles")
@Api(tags = "角色管理")
public class SysRoleController {
    @Resource
    private ISysRoleService sysRoleService;
    @GetMapping
    @ApiOperation(value = "获取角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "每页条数"),
            @ApiImplicitParam(name = "name", value = "角色名称"),
    })
    @PreAuthorize("hasAuthority('sys_role_query')")
    public Result<Page<SysRole>> getRoles(Page<SysRole> page, String name){
        page.addOrder(OrderItem.desc("last_update_time"));
        return Result.success(sysRoleService.findRoles(page,name));
    }
    @PostMapping
    @ApiOperation(value = "新增角色")
    @PreAuthorize("hasAuthority('sys_role_create')")
    public Result<?> saveRoles(@RequestBody @Validated SysRole sysRole){
        if(sysRoleService.save(sysRole)){
            return Result.success("新增成功");
        }else{
            return Result.fail("新增失败");
        }
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "角色id数组"),
    })
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public Result<?> deleteRoles(@RequestBody String[] ids){
       return sysRoleService.removeRolesByIds(ids);
    }

}
